// ==UserScript==
// @name         点击按钮发送Cookies至后台
// @namespace    响应网址
// @version      2.0
// @description  点击按钮发送Cookies至后台
// @match        http://*/alarmOrderCount/alarmsHandle
// @match        http://*/cs/index.html
// @match        http://*/work/todo
// @match        https://*/webframe/*
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// @connect      *
// ==/UserScript==

(function() {
    let globalCbssMsGrayValue = null;

    // 拦截并捕获 CBSS-MS-GRAY 值
    (function captureHeaderValue() {
        const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
        XMLHttpRequest.prototype.setRequestHeader = function(key, value) {
            if (key === 'CBSS-MS-GRAY') {
                globalCbssMsGrayValue = value;
                console.log('CBSS-MS-GRAY:', globalCbssMsGrayValue);
            }
            originalSetRequestHeader.apply(this, arguments);
        };
    })();

    // 发送数据到后台
    function sendData(url, data, successMessage) {
        GM_xmlhttpRequest({
            method: "POST",
            url: url,
            anonymous: true,
            headers: { "Content-Type": "application/json" },
            data: JSON.stringify(data),
            onload: function(response) {
                console.log('响应:', response.responseText);
                alert(successMessage);
            },
            onerror: function() {
                alert('发送失败，请检查后台服务是否启动');
            }
        });
    }

    // 发送客服 Cookies 和相关数据
    function sendKefuCookies() {
        const cookieValue = document.cookie;
        const authorization = sessionStorage.getItem("permissionSecCertSession");

        if (cookieValue && authorization && globalCbssMsGrayValue) {
            const data = {
                Authorization: "Bearer " + authorization,
                cookies: cookieValue,
                CBSS_MS_GRAY: globalCbssMsGrayValue
            };
            sendData("http://127.0.0.1:5555/kefu", data, '客服 Cookies 已发送至后台！');
        } else {
            alert('未找到完整的Cookies或授权信息！');
        }
    }

    // 发送普通 Cookies 和 URL 信息
    function sendGenericCookies() {
        const cookieValue = document.cookie;
        const currentURL = document.URL;

        if (cookieValue) {
            const data = {
                cookies: cookieValue,
                URL: currentURL
            };
            sendData("http://127.0.0.1:5555/cookie", data, 'cmp或网管或联通数科 Cookies 已发送至后台！');
        } else {
            alert('未找到 Cookies！');
        }
    }

    // 创建按钮并绑定点击事件
    function createButton(text, onClickHandler) {
        const button = document.createElement('button');
        button.textContent = text;
        button.onclick = onClickHandler;
        return button;
    }

    // 创建并显示悬浮按钮面板
    function createPanel() {
        const panel = document.createElement('div');
        panel.classList.add('panel');
        panel.style.display = 'none';

        const kefuButton = createButton('发送客服 Cookies', sendKefuCookies);
        const jicheng = createButton('发送cmp或网管或联通数科 Cookies ', sendGenericCookies);

        panel.appendChild(kefuButton);
        panel.appendChild(jicheng);

        document.body.appendChild(panel);
        return panel;
    }

    // 切换面板显示
    function togglePanelDisplay(panel) {
        panel.style.display = (panel.style.display === 'none') ? 'block' : 'none';
    }

    // 创建面板和切换按钮
    function initUI() {
        const panel = createPanel();
        const toggleButton = createButton('展开', () => togglePanelDisplay(panel));
        toggleButton.classList.add('toggle-button');
        document.body.appendChild(toggleButton);
        // 添加样式
        const style = document.createElement('style');
        style.textContent = `
            .panel {
                position: fixed;
                bottom: 20px;
                right: 20px;
                width: 300px;
                padding: 10px;
                background-color: white;
                border: 1px solid #ccc;
                box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.3);
            }
            .toggle-button {
                position: fixed;
                bottom: 70px;
                display: block;
                margin-bottom: 10px;
                padding: 10px;
                color: white;
                border: none;
                cursor: pointer;
                right: 20px;
                z-index: 1000;
            }
            .panel button {
                display: block;
                margin-bottom: 10px;
                padding: 10px;
                color: white;
                border: none;
                cursor: pointer;
            }
            .panel button:hover {
                background-color: #45a049;
            }
            .toggle-button:hover {
                background-color: #45a049;
            }
        `;
        document.head.appendChild(style);
    }

    // 初始化界面
    initUI();
})();
